package com.gzwanchuang.xhgj.ui.page.mine;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.gzwanchuang.architecture.utils.ValidateUtils;
import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.data.repository.Apis;
import com.gzwanchuang.xhgj.bridge.request.MineRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.mine.HeXiaoChaNextViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentHeXiaoChaNextBinding;
import com.gzwanchuang.xhgj.net.NetworkManager;
import com.gzwanchuang.xhgj.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.xhgj.proto.Mergedintoaccount;
import com.gzwanchuang.xhgj.ui.base.BaseFragment;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class HeXiaoChaNextFragment extends BaseFragment {
    private FragmentHeXiaoChaNextBinding mBinding;
    private HeXiaoChaNextViewModel mViewModel;
    private MineRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(HeXiaoChaNextViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(MineRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_he_xiao_cha_next, container, false);
        mBinding = FragmentHeXiaoChaNextBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            showShortToast(s);
        });

        mRequestViewModel.getResultString().observe(this, s -> {
            showShortToast(s);
            nav().navigateUp();
        });
        mViewModel.money.set(getArguments().getString("money"));
        mViewModel.bishu.set("总交易笔数：" + getArguments().getString("bishu") + "笔");
        String order = getArguments().getString("order", "");
        Log.e("order",order);
        if (ValidateUtils.isValidate(order)) {
            Mergedintoaccount.order_confirm.Builder builders = Mergedintoaccount.order_confirm.newBuilder();
            builders.setOrderNo(order);
            NetworkManager.INSTANCE.post(Apis.order_confirm, builders.build().toByteArray(), new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) {

                }

                @Override
                public void onError(int errorCode, String errorMessage) {
                    showShortToast(errorMessage);
                }
            });
        }



    }


    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void submit() {
            nav().navigateUp();
            mSharedViewModel.isCheck.setValue(true);
        }
    }
}
